package com.todaynews.ui.video;

import android.view.ViewGroup;

import androidx.viewpager.widget.ViewPager;

import com.todaynews.R;
import com.todaynews.frame.model.InfoResult;
import com.todaynews.frame.ui.barlibrary.ImmersionBar;
import com.todaynews.frame.ui.fragment.BaseFragment;
import com.todaynews.frame.ui.views.colortrackview.ColorClipTabLayout;
import com.todaynews.model.model.Channel;
import com.todaynews.ui.video.adapter.VideoChannelPagerAdapter;
import com.todaynews.ui.video.model.VideoChannel;
import com.todaynews.utils.screen.DensityUtil;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

public class VideoFragment extends BaseFragment {

    @BindView(R.id.tab_channel)
    ColorClipTabLayout tabChannel;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private List<VideoChannel> videoChannels = new ArrayList<>();
    private VideoChannelPagerAdapter videoChannelPagerAdapter;
    private List<VideosListFragment> videosListFragmentList = new ArrayList<>();
    private List<Channel> channelList = new ArrayList<>();

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.white)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_video;
    }

    @Override
    public void init() {
        getChannelData();
        getVideoChannelFragment();
        initData();
    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void getChannelData() {
        String[] channels = getResources().getStringArray(R.array.channel_video);
        String[] channelCodes = getResources().getStringArray(R.array.channel_code_video);
        //默认添加了全部频道
        for (int i = 0; i < channelCodes.length; i++) {
            String title = channels[i];
            String code = channelCodes[i];
            videoChannels.add(new VideoChannel(title, code));
        }
    }

    private void getVideoChannelFragment(){
        for (int i = 0; i < videoChannels.size(); i++) {
            VideosListFragment videosListFragment = VideosListFragment.newInstance(videoChannels.get(i).getCode());
            videosListFragmentList.add(videosListFragment);//添加到集合中
        }
    }

    private void initData(){
        videoChannelPagerAdapter = new VideoChannelPagerAdapter(videoChannels,getChildFragmentManager());
        vpContent.setAdapter(videoChannelPagerAdapter);
        vpContent.setOffscreenPageLimit(2);
        tabChannel.setTabPaddingLeftAndRight(DensityUtil.getInstance().dpToPx(7), DensityUtil.getInstance().dpToPx(10));
        tabChannel.setupWithViewPager(vpContent);
        tabChannel.post(new Runnable() {
            @Override
            public void run() {
                //设置最小宽度，使其可以在滑动一部分距离
                ViewGroup slidingTabStrip = (ViewGroup) tabChannel.getChildAt(0);
                slidingTabStrip.setMinimumWidth(slidingTabStrip.getMeasuredWidth());
            }
        });
        //隐藏指示器
        tabChannel.setSelectedTabIndicatorHeight(0);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //当页签切换的时候，如果有播放视频，则释放资源
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public String getCurrentChannelCode(){
        int currentItem = vpContent.getCurrentItem();
        return channelList.get(currentItem).channelCode;
    }
}
